home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / usr / sbin / foomatic-preferred-driver < prev    next >
Text File  |  2008-08-19  |  9KB  |  349 lines

  1. #!/usr/bin/perl
  2.  
  3. # This is foomatic-preferred driver, it reads the printer database
  4. # overview and puts default driver entries (<driver>..</driver>) into
  5. # every printer's XML database entry.
  6.  
  7. use Foomatic::Defaults;
  8. use Foomatic::DB qw/get_overview/;
  9. use Data::Dumper;
  10. use strict;
  11. # Needs "get_overview" to be added to the "@EXPORT_OK" list of DB.pm!
  12.  
  13. # Read out the program name with which we were called, but discard the path
  14. $0 =~ m!/([^/]+)\s*$!;
  15. my $progname = $1;
  16. my $debug = 0;
  17.  
  18. my $db = new Foomatic::DB;
  19.  
  20. # Get the printer overview list as a Perl data structure
  21. $db->get_overview();
  22. print "DB " . Dumper($db ) if $debug;
  23.  
  24. my $printer;
  25. for $printer (@{$db->{'overview'}}) {
  26.     # Skip printers without XML file. Here we cannot set a recommended
  27.     # driver
  28.     next if $printer->{'noxmlentry'};
  29.     print "$printer->{'make'} $printer->{'model'} ($printer->{'id'})";
  30.     if ($printer->{'driver'}) { # We have already a default driver
  31.     # Check whether this driver is one which supports the printer
  32.     # If yes, this printer has a correct default driver so we do not
  33.     # need to set a new default driver
  34.     my $found = 0;
  35.     for my $d (@{$printer->{'drivers'}}) {
  36.         if ($d eq $printer->{'driver'}) {
  37.         $found = 1;
  38.         last;
  39.         }
  40.     }
  41.     if ($found) {
  42.         print " (Keeping old default driver): $printer->{'driver'}\n";
  43.         next;
  44.     } else {
  45.         print " (Default driver wrong)\n";
  46.     }
  47.     } else {
  48.     print " (No default driver defined)\n";
  49.     }
  50.     if ($printer->{'functionality'} eq 'F') {
  51.     # If the printer is a "Paperweight", do not correct the
  52.     # default driver, remove the wrong setting and skip the 
  53.     # printer
  54.     $printer->{'driver'} = "";
  55.     print ": --> Skipping \"Paperweight\"\n";
  56.     } else {
  57.     #Sort drivers appropriate to ranking list
  58.     my @sorted = sort sortdrivers @{$printer->{'drivers'}};
  59.     print ": @sorted --> $sorted[0]\n";
  60.     $printer->{'driver'} = $sorted[0];
  61.     }
  62.     open PRINTERENTRY, "< $libdir/db/source/printer/$printer->{'id'}.xml" || die "   Database entry for the printer $printer->{'make'} $printer->{'model'} ($printer->{'id'}) cannot be read!\n";
  63.     my  @printerentryfield = <PRINTERENTRY>;
  64.     close PRINTERENTRY;
  65.     
  66.     my $printerentry = join ('', @printerentryfield);
  67.     if ($printerentry =~ m!<driver>(.*)</driver>!s) {
  68.     $printerentry =~ 
  69.       s!<driver>(.+)</driver>!<driver>$printer->{'driver'}</driver>!sg;
  70.     } elsif ($printerentry =~ m=(<!\-\-unknown preferred "driver"\-\->)=s){
  71.     $printerentry =~ 
  72.       s=(<!\-\-unknown preferred "driver"\-\->)=\n  <driver>$printer->{'driver'}</driver>=sg;
  73.     } elsif ($printerentry =~ m!<unverified\s*/>!s) {
  74.     $printerentry =~
  75.       s!<unverified\s*/>!<unverified />\n  <driver>$printer->{'driver'}</driver>!sg;
  76.     } elsif ($printerentry =~ m!</functionality>!s) {
  77.     $printerentry =~
  78.       s!</functionality>!</functionality>\n  <driver>$printer->{'driver'}</driver>!sg;
  79.     } else {
  80.     $printerentry =~
  81.       s!</model>!</model>\n  <driver>$printer->{'driver'}</driver>!sg;
  82.     }
  83.     open PRINTERENTRY, "> $libdir/db/source/printer/$printer->{'id'}.xml" || die "   Database entry for the printer $printer->{'make'} $printer->{'model'} ($printer->{'id'}) cannot be written!\n";
  84.     print PRINTERENTRY $printerentry;
  85.     close PRINTERENTRY;
  86. }
  87.  
  88. exit 0;
  89.  
  90. sub sortdrivers ($$) {
  91.  
  92.     # All sorting done case-sensitive and characters which are not a letter
  93.     # or number are taken out!!
  94.  
  95.     # List of driver names in a ranking which driver should be preferred
  96.     # against other drivers.
  97.     # The terms must fit to the beginning of the line, terms which must fit
  98.     # exactly must have '\$' in the end.
  99.     my @drivers =      (
  100.             # Laser printers (except winprinters)
  101.             "epl5800", # Additional functionality for Epson
  102.             "epl2050", # Laser printers
  103.             "epl2050p",
  104.             "hl1250", # Special Brother drivers should be preferred
  105.             "hl1240", # for Brother printers
  106.                         "Postscript", # Tray selection, 1200+ dpi, color laser
  107.             "alc2000", # Epson color lasers print in color 
  108.             "alc8500", # with these drivers from Epson
  109.             "cljet5\$", # color laser PCL
  110.             "cljet5c",
  111.             "pxlcolor", # 1200 dpi color laser PCL
  112.             "pxlmono", # 1200 dpi, faster than "lj5gray"
  113.             "epl5900", # This driver has a bug, "pxlmono" works
  114.                    # correctly 
  115.             "lj5gray", # 1200 dpi
  116.             "lj5mono",
  117.             "ljet4\$", # HP laser drivers, the newer, the better
  118.             "ljet4d",
  119.             "lj4dithp",
  120.             "lj4dith\$",
  121.             "hl7x0", # Brother driver, proprietary language,
  122.                  # to prefer when there is also PCL
  123.                  # emulation
  124.             "ljet3d",
  125.             "ljet3\$",
  126.             "ljet2p",
  127.             "ljetplus",
  128.             "laserjet",
  129.             # HP PPA inkjets, when a printer is supported by one
  130.             # of these drivers, it is for sure not supported by
  131.             # any other driver.
  132.             "pnm2ppa", # color
  133.             "pbm2ppa", # bw
  134.             # Lexmark 3200, the Gutenprint support for this
  135.             # printer is broken
  136.             "lxm3200-tweaked",
  137.             "lxm3200\$",
  138.             "lxm3200c",
  139.             "lxm3200m",
  140.             "lxm3200p",
  141.             # Canon BJC-8200, these UPPs are probably better
  142.             # than Gutenprint
  143.             "bj8pa06n.upp",
  144.             "bj8oh06n.upp",
  145.             "bj8ts06n.upp",
  146.             "bj8gc12f.upp",
  147.             "bj8hg12f.upp",
  148.             "bj8pp12f.upp",
  149.             # HP and compatible inkjets
  150.             "hpijs-rss", # HP drivers, patched
  151.             "hpijs", # HP drivers, with Duplex and 1200 dpi
  152.             "cdj970", # Duplex support
  153.             "DJ", # HP drivers, but no duplex
  154.             "chp2200", # HP driver
  155.             "gutenprint-ijs", # IJS plug-in driver of Gutenprint
  156.             "gutenprint", # Tray selection on laser, best 
  157.                       # quality on inkjets (for HP HP's
  158.                       # driver is better)
  159.             "gimp-print-ijs", # Old Gimp-Print driver
  160.             "gimp-print",
  161.             "stp", # Predecessor of Gimp-Print/Gutenprint
  162.             "cdj1600", # Relatively good drivers for HP inkjets
  163.             "cdj890",  # not covered by Gutenprint or the HP
  164.             "cdj880",  # drivers
  165.             "cdj850",
  166.             "cdj670",
  167.             "pcl3", # When nothing else works ...
  168.             "hpdj", # Predecessor of pcl3
  169.             "sharp.upp", # Gets Sharp inkjets to work without
  170.                          # a lot of hassle
  171.             "cdj550\$",
  172.             "cdj550.upp",
  173.             "cdj500",
  174.             "djet500", # Somewhat old-fashioned
  175.             "deskjet",
  176.             # Canon inkjets
  177.             "s400a1.upp", # New printer, UPPs are probably 
  178.             "s400b1.upp", # better than "bjc600"/"bjc800"
  179.             "bjc800", # More flexible than the Uniprint drivers
  180.             "bjc600",
  181.             "bj200",
  182.             "bj10e",
  183.             "bjc6000a1.upp",
  184.             "bjc6000b1.upp",
  185.             "bjc610a0.upp",
  186.             "bjc610a1.upp",
  187.             "bjc610a2.upp",
  188.             "bjc610a3.upp",
  189.             "bjc610a4.upp",
  190.             "bjc610a5.upp",
  191.             "bjc610a6.upp",
  192.             "bjc610a7.upp",
  193.             "bjc610a8.upp",
  194.             "bjc610b1.upp",
  195.             "bjc610b2.upp",
  196.             "bjc610b3.upp",
  197.             "bjc610b4.upp",
  198.             "bjc610b6.upp",
  199.             "bjc610b7.upp",
  200.             "bjc610b8.upp",
  201.             # Epson inkjets
  202.             "st640ih.upp",
  203.             "st640ihg.upp",
  204.             "st640p.upp",
  205.             "st640pg.upp",
  206.             "st640pl.upp",
  207.             "st640plg.upp",
  208.             "stc640p.upp",
  209.             "Stp720p.upp",
  210.             "Stp720pl.upp",
  211.             "Stp870p.upp",
  212.             "Stp870pl.upp",
  213.             "stcolor",
  214.             "st800",
  215.             "PM760p.upp",
  216.             "PM760pl.upp",
  217.             "PM820p.upp",
  218.             "PM820pl.upp",
  219.             "Stc670p.upp",
  220.             "Stc670pl.upp",
  221.             "Stc680p.upp",
  222.             "Stc680pl.upp",
  223.             "Stc760p.upp",
  224.             "Stc760pl.upp",
  225.             "Stc777p.upp",
  226.             "Stc777pl.upp",
  227.             "stc1520h.upp",
  228.             "stc800ih.upp",
  229.             "stc800p.upp",
  230.             "stc800pl.upp",
  231.             "stc740ih.upp",
  232.             "stc740p.upp",
  233.             "stc740pl.upp",
  234.             "stc600ih.upp",
  235.             "stc600p.upp",
  236.             "stc600pl.upp",
  237.             "stc500p.upp",
  238.             "stc500ph.upp",
  239.             "stc300.upp",
  240.             "stc300bm.upp",
  241.             "stc300bl.upp",
  242.             "stc2.upp",
  243.             "stc2_h.upp",
  244.             "stc2s_h.upp",
  245.             "stc.upp",
  246.             "stc_h.upp",
  247.             "stc_l.upp",
  248.             "stcany.upp",
  249.             # Lexmark inkjets
  250.             "lz11",
  251.             "cZ11somsom",
  252.             "cZ11",
  253.             "lx5000",
  254.             "lex7000",
  255.             "lex5700",
  256.             "lx5700m",
  257.             "pbm2l7k",
  258.             # Thermo sublimation printers
  259.             "ppmtomd", # The successor of "ppmtocpva"
  260.             "ppmtocpva",
  261.             "md5k",
  262.             "md2k",
  263.             # Others
  264.             "t4693d8",
  265.             "t4693d4",
  266.             "t4693d2",
  267.             "necp2x.upp",
  268.             "necp2x6.upp",
  269.             "declj250", # More adjustable options
  270.             "lj250",
  271.             "pj", # More adjustable options
  272.             "paintjet",
  273.             "pjxl", # More adjustable options
  274.             "pjetxl",
  275.             "pjxl300",
  276.             "iwhi",
  277.             "iwlo",
  278.             "necp6",  # in most cases the NEC PinWriter P6 (+)
  279.             "epsonc", # is used without colour add-on.
  280.             "epson",
  281.             "eps9high",
  282.             "eps9mid",
  283.             "lq850",
  284.             "cjet",
  285.             "lbp8",
  286.             "oki182",
  287.             "ibmpro",
  288.             "gdi",
  289.             "pbmtozjs",
  290.             # Japanese printer drivers
  291.             "rpdl",
  292.             "pr201",
  293.             "pr150",
  294.             "picty180",
  295.             "npdl",
  296.             "ml600",
  297.             "mj8000c",
  298.             "mj700v2c",
  299.             "mj6000c",
  300.             "mj500c",
  301.             "md50Mono",
  302.             "md50Eco",
  303.             "md1xMono",
  304.             "lp2000",
  305.             "lj4dithp",
  306.             "lips4v",
  307.             "lips4",
  308.             "lips2p",
  309.             "lbp320",
  310.             "lbp310",
  311.             "jj100",
  312.             "fmpr",
  313.             "fmlbp",
  314.             "filter800_3300",
  315.             "filter800",
  316.             "filter770",
  317.             "filter760",
  318.             "escpage",
  319.             "dj505j",
  320.             "bjc880j",
  321.             "bjc800j",
  322.             "bj10vh",
  323.             "bj10v",
  324.             "bj10",
  325.             "sj48",
  326.             # Omni is in experimental state, so other drivers
  327.             # give probably better output
  328.             "omni"
  329.             );
  330.  
  331.     my $first = $_[0];
  332.     my $second = $_[1];
  333.     # The driver names to compare are absolutely equal ->
  334.     #    Bug in database ==> ignore
  335.     if ($first eq $second) {return 0};
  336.  
  337.     # Check whether they are in the @standardopts list
  338.     my $i;
  339.     for ($i = 0; $i <= $#drivers; $i++) {
  340.         my $firstinlist = ($first =~ /^$drivers[$i]/);
  341.         my $secondinlist = ($second =~ /^$drivers[$i]/);
  342.         if (($firstinlist) && (!$secondinlist)) {return -1};
  343.         if (($secondinlist) && (!$firstinlist)) {return 1};
  344.     }
  345.  
  346.     # No preference found, stop here
  347.     die "No preference found between $first and $second!\n";
  348. }
  349.